package com.example.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.example.domain.Person;
import com.example.domain.PersonAdapter;
import com.example.service.PersonService;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ListView listView;
	private PersonService personService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        personService=new PersonService(getApplicationContext());
        listView=(ListView)this.findViewById(R.id.listView);
        listView.setOnItemClickListener(new ItemClickListener());
        show2();
    }
    private final class ItemClickListener implements OnItemClickListener
    {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			//System.out.println("eeeeee");
			// TODO Auto-generated method stub
			ListView lView=(ListView)parent;
			//Person person=(Person)lView.getItemAtPosition(position);
			//Toast.makeText(getApplicationContext(), person.getName().toString(), 1).show();
			/*ListView lView=(ListView)parent;
			lview=(ListView).
			*/
		}
    }
    private void show3()
    {
    	List<Person> persons=personService.getScrollData(0, 20);
    	PersonAdapter adapter=new PersonAdapter(getApplicationContext(), persons, R.layout.item);
    	listView.setAdapter(adapter);
    }
    private void show2()
    {
    	Cursor cursor=personService.getCursorScrollData(0, 6);
    	SimpleCursorAdapter adapter=new SimpleCursorAdapter(getApplicationContext(), R.layout.item, cursor, new String[]{"name","phone","age"},new int[]{R.id.name,R.id.phone,R.id.age});
    	listView.setAdapter(adapter);
    }

    private void show() {
		// TODO Auto-generated method stub
		List<Person> persons=personService.getScrollData(0, 20);
		List<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();
		for(int i=0;i<persons.size();i++)
		{
			HashMap<String, Object> item=new HashMap<String, Object>();
			item.put("name", persons.get(i).getName());
			item.put("phone", persons.get(i).getPhone());
			item.put("age", persons.get(i).getAge());
			data.add(item);
		}
		SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(), data, R.layout.item, new String[]{"name","phone","age"},new int[]{R.id.name,R.id.phone,R.id.age});
		listView.setAdapter(adapter);
		adapter.getCount();
		/*int total=adapter.getCount();
		int perpage=7;
		for(int i=0;i<perpage;i++)
		{
			View view=adapter.getView(i, convertView, parent)
		}*/
	}
    public void insert()
    {
    	for(int i=0;i<10;i++)
		{
			Person p=new Person();
			p.setAge(i+5);
			p.setName("name"+String.valueOf(i));
			p.setPhone("1512104500"+String.valueOf(i));
			PersonService service=new PersonService(getApplicationContext());
			service.save(p);
			System.out.println(i);
		}
    }


	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
